﻿@model DHQGHN.PM2.ViewModels.MappingPTDTViewModel
@{
    ViewBag.Title = "Chọn phương thức điều tra";
    ViewBag.Group = "Quản lý chung";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<link href="~/Content/standard_form_nhaplieu.css" rel="stylesheet" />
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

<script>
    $(function () {
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }

        $(document).ready(function () {
            if ('@ViewBag.Level3' != '') {
                $('#Level1').empty();
                $('#Level2').empty();
                $('#Level3').empty();
                $('#Level1').prop('disabled', false);
                $('#Level2').prop('disabled', false);
                $('#Level3').prop('disabled', false);
                $('#divLoading').show();
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel1")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    success: function (lstItems) {
                        $('#Level1').show();
                        $('#divLoading').hide();
                        $('#Level1').append('<option value = "-1">' +
                                     "--Chọn Tỉnh--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            if (lstItem.Value == '@ViewBag.Level1') {
                                $('#Level1').append('<option value="' + lstItem.Value + '" selected = "selected">' +
                                lstItem.Text + '</option>');
                            } else {
                                $('#Level1').append('<option value="' + lstItem.Value + '">' +
                                     lstItem.Text + '</option>');
                            }
                        });
                    },
                    error: function (ex) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                });
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel2")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: '@ViewBag.Level1' },
                    success: function (lstItems) {
                        $('#Level2').show();
                        $('#divLoading').hide();
                        $('#Level2').append('<option value = "-1">' +
                                     "--Chọn Huyện--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            if (lstItem.Value == '@ViewBag.Level2') {
                                $('#Level2').append('<option value="' + lstItem.Value + '" selected = "selected">' +
                                lstItem.Text + '</option>');
                            } else {
                                $('#Level2').append('<option value="' + lstItem.Value + '">' +
                                     lstItem.Text + '</option>');
                            }
                        });
                    },
                    error: function (ex) {
                        alert('Lỗi kỹ thuật.' + ex);
                        $('#divLoading').hide();
                    }
                });
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel3")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: '@ViewBag.Level2' },
                    success: function (lstItems) {
                        $('#Level3').show();
                        $('#divLoading').hide();
                        $('#Level3').append('<option value = "-1">' +
                                     "--Chọn Xã--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            if (lstItem.Value == '@ViewBag.Level3') {
                                $('#Level3').append('<option value="' + lstItem.Value + '" selected = "selected">' +
                                lstItem.Text + '</option>');
                            } else {
                                $('#Level3').append('<option value="' + lstItem.Value + '">' +
                                     lstItem.Text + '</option>');
                            }
                        });
                    },
                    error: function (ex) {
                        alert('Lỗi kỹ thuật.' + ex);
                        $('#divLoading').hide();
                    }
                });
                $.ajax({
                    url: '@Url.Action("MappingPTDT", "HouseHold")',
                    cache: false,
                    type: "GET",
                    dataType: 'html',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: '@ViewBag.Level3' },
                    success: function (result) {
                        $('#divLoading').hide();
                        $('#mappingDiv').show();
                        $('#mappingDiv').html('');
                        $('#mappingDiv').html(result);
                    },
                    error: function (ex, ajaxOptions, thrownError) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                });
                $('#msg').append("Gán thành công!");
            }
            if ('@ViewBag.Level1' == '') {
                $('#Level2').empty();
                $('#Level3').empty();
                $('#Level1').prop('disabled', false);
                $('#Level2').prop('disabled', true);
                $('#Level3').prop('disabled', true);
                $('#mappingDiv').empty();
                $('#mappingDiv').hide();
                $('#divLoading').show();
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel1")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    success: function (lstItems) {
                        $('#divLoading').hide();
                        $('#Level1').show();
                        $('#LEVELS').val('1');
                        $('#Level1').append('<option value = "-1" selected="selected">' +
                                     "--Chọn Tỉnh--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            $('#Level1').append('<option value="' + lstItem.Value + '">' +
                                 lstItem.Text + '</option>');
                        });
                    },
                    error: function (ex) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                })
            }
            return false;
        });
        $('#Level1').change(function () {
            $('#Level2').empty();
            $('#Level3').empty();
            $('#Level1').prop('disabled', false);
            $('#Level2').prop('disabled', false);
            $('#Level3').prop('disabled', true);
            $('#mappingDiv').empty();
            $('#mappingDiv').hide();
            $('#divLoading').show();
            var parentCode = $('#Level1').val();

            if (parentCode != '-1') {
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel2")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: parentCode },
                    success: function (lstItems) {
                        $('#divLoading').hide();
                        $('#Level2').show();
                        $('#LEVELS').val('2');
                        $('#Level2').append('<option value = "-1" selected="selected">' +
                                     "--Chọn Huyện--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            $('#Level2').append('<option value="' + lstItem.Value + '">' +
                                 lstItem.Text + '</option>');
                        });
                    },
                    error: function (ex) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                })
                return false;
            } else {
                $('#Level2').empty();
                $('#Level3').empty();
                $('#Level1').prop('disabled', false);
                $('#Level2').prop('disabled', true);
                $('#Level3').prop('disabled', true);
                $('#mappingDiv').empty();
                $('#mappingDiv').hide();
                $('#AdminCode').empty();
                $('#divLoading').hide();
            }
        });
        $('#Level2').change(function () {
            $('#Level1').prop('disabled', false);
            $('#Level2').prop('disabled', false);
            $('#Level3').prop('disabled', false);
            $('#Level3').empty();
            $('#mappingDiv').empty();
            $('#mappingDiv').hide();
            $('#divLoading').show();
            var parentCode = $('#Level2').val();

            if (parentCode != '-1') {
                $.ajax({
                    url: '@Url.Action("ddlAdmUnitLevel3")',
                    type: 'POST',
                    datatype: 'json',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: parentCode },
                    success: function (lstItems) {
                        $('#divLoading').hide();
                        $('#Level3').show();
                        $('#LEVELS').val('3');
                        $('#Level3').append('<option value = "-1" selected="selected">' +
                                     "--Chọn Xã--" + '</option>');
                        $.each(lstItems, function (i, lstItem) {
                            $('#Level3').append('<option value="' + lstItem.Value + '">' +
                                 lstItem.Text + '</option>');
                        });
                    },
                    error: function (ex) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                })
                return false;
            } else {
                $('#Level3').empty();
                $('#mappingDiv').empty();
                $('#mappingDiv').hide();
                $('#Level1').prop('disabled', false);
                $('#Level2').prop('disabled', false);
                $('#Level3').prop('disabled', true);
                $('#AdminCode').empty();
                $('#divLoading').hide();
            }
        });
        $('#Level3').change(function () {
            $('#divLoading').show();
            var parentCode = $('#Level3').val();
            if (parentCode != "-1") {
                $.ajax({
                    url: '@Url.Action("MappingPTDT", "HouseHold")',
                    cache: false,
                    type: "GET",
                    dataType: 'html',
                    headers: {
                        'VerificationToken': '@GetAntiForgeryToken()'
                    },
                    data: { parentCode: parentCode },
                    success: function (result) {
                        $('#divLoading').hide();
                        $('#mappingDiv').show();
                        $('#mappingDiv').html('');
                        $('#mappingDiv').html(result);
                    },
                    error: function (ex, ajaxOptions, thrownError) {
                        $('#divLoading').hide();
                        alert('Lỗi kỹ thuật.' + ex);
                    }
                });
            } else {
                $('#divLoading').hide();
                $('#mappingDiv').empty();
                $('#mappingDiv').hide();
            }
        });
    });
</script>
@Html.HiddenFor(model => model.LEVELS)

<div class="page-bar">
    <ul class="page-breadcrumb" style="padding: 4px;">
        <li>
            <i class="fa fa-home"></i>
            <a href="#"> Chọn phương thức điều tra</a>
        </li>
    </ul>
</div>

<div>
    <div class="row">
        <div class="col-md-1 col-sm-2" style="padding: 0px 0px 0px 15px; white-space: nowrap;">
            @Html.Label("Cấp Tỉnh", new { @class = "form-control", style = "text-align: left; border: none;" })
        </div>
        <div class="col-md-3 col-sm-4" style="margin-bottom:10px;">
        @Html.DropDownList("Level1", new SelectList(string.Empty, "Value", "Text"), new { id = "Level1", @class = "form-control" })</div>

        <div class="col-md-1 col-sm-2" style="padding: 0px 0px 0px 15px; white-space: nowrap;">
            @Html.Label("Cấp Huyện", new { @class = "form-control", style = "text-align: left; border: none;" })
        </div>
        <div class="col-md-3 col-sm-4" style="margin-bottom:10px;">
        @Html.DropDownList("Level2", new SelectList(string.Empty, "Value", "Text"), new { id = "Level2", @class = "form-control", disabled = "disabled" })</div>

        <div class="col-md-1 col-sm-2" style="padding: 0px 0px 0px 15px; white-space: nowrap;">
            @Html.Label("Cấp Xã", new { @class = "form-control", style = "text-align: left; border: none;" })
        </div>
        <div class="col-md-3 col-sm-4" style="margin-bottom:10px;">
            @Html.DropDownList("Level3", new SelectList(string.Empty, "Value", "Text"), new { id = "Level3", @class = "form-control", disabled = "disabled" })
        </div>
    </div>
    <div id="mappingDiv"></div>
</div>
